<?php
session_start();
require('../check_login.php');
include(dirname(__FILE__) . '/../config.php');
require_once('supports_lib.php');
if (isset($_REQUEST['id'])) {
    $a_contact = get_support_detail($_REQUEST['id']);
}
if (isset($_POST['go'])) {
    add_support_comment($_POST);
    //var_dump($_POST);die();
    if($_POST['status']==3){
        $support = get_support_detail($_REQUEST['id']);
        $post['status']=3;
        $post['id']=$support['id'];
        $post['support_name']=$support['support_name'];
        $post['description']=$support['description'];
        $post['date_updated']=time();
        $post['product_id']=$support['product_id'];
        $post['assigned_to']=$support['creator'];
        $post['creator']=$support['creator'];
        update_support($post);
    }
}
require_once('../header.php');
?>

<!-- BEGIN CONTAINER -->
<div class="page-container">
    <!-- BEGIN SIDEBAR -->
    <?php require_once('../left_menu.php'); ?>
    <!-- END SIDEBAR -->
    <!-- BEGIN CONTENT -->
    <div class="page-content-wrapper">
        <div class="page-content">
            <!-- BEGIN SAMPLE PORTLET CONFIGURATION MODAL FORM-->

            <!-- /.modal -->
            <!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->
            <!-- BEGIN STYLE CUSTOMIZER -->

            <!-- END STYLE CUSTOMIZER -->
            <!-- BEGIN PAGE HEADER-->
            <div class="row">
                <div class="col-md-12">
                    <!-- BEGIN PAGE TITLE & BREADCRUMB-->

                    <ul class="page-breadcrumb breadcrumb">

                        <li>
                            <i class="fa fa-home"></i>
                            <a href="<? echo $url ;?>/dashboard.php">
                                Trang chủ
                            </a>
                            <i class="fa fa-angle-right"></i>
                        </li>
                        <li>
                            <a href="<?php echo $url; ?>/support/supports.php">
                                Danh sách các vấn đề cần giải quyết
                            </a>
                            <i class="fa fa-angle-right"></i>
                        </li>
                        <li>
                            <a href="#">
                                Xem chi tiết
                            </a>
                            <i class="fa fa-angle-right"></i>
                        </li>

                    </ul>
                    <!-- END PAGE TITLE & BREADCRUMB-->
                </div>
            </div>

            <!-- END PAGE HEADER-->
            <!-- BEGIN PAGE CONTENT-->
            <div class="portlet box green">
                <div class="portlet-title">

                </div>
                <div class="portlet-body form">
                    <!-- BEGIN FORM-->
                    <form id ='form_add_comment' class="horizontal-form" method="post" action="view_support_detail.php">
                        <div class="form-body">
                            <h3 class="form-section">Thông tin chung</h3>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="control-label">Tiêu đề:

                                        </label>
                                        <label class="control-label">
                                            <?php echo (isset($a_contact['support_name']) ? $a_contact['support_name'] : '') ?>
                                        </label>


                                    </div>
                                </div>

                            </div>

                            <div class="row">

                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="control-label">Tình trạng:

                                        </label>
                                        <label class="control-label">

                                            <?php
                                            switch ($a_contact['status']) {
                                                case '1':
                                                    echo 'Tạo mới';
                                                    break;
                                                case '2':
                                                    echo 'Đang được xử lý';
                                                    break;
                                                case '3':
                                                    echo 'Đã được giải quyết';
                                                    break;
                                                default:
                                                    break;
                                            }
                                            ?>
                                        </label>

                                    </div>
                                </div>


                            </div>

                            <div class="row">

                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="control-label">Người phụ trách:

                                        </label>
                                        <label class="control-label">
                                            <?php
                                            include_once(dirname(__FILE__) . '/../user/users_lib.php');
                                            $user = get_user_detail($a_contact['assigned_to']);
                                            ?>
                                            <?php echo $user['first_name'] . ' ' . $user['last_name'] ?>
                                        </label>

                                    </div>
                                </div>


                            </div>

                            <div class="row">

                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="control-label">Thuộc sản phẩm:

                                        </label>
                                        <label class="control-label">
                                            <?php
                                            include_once(dirname(__FILE__) . '/../product/products_lib.php');
                                            $product = get_product_detail($a_contact['product_id']);
                                            ?>
                                            <?php echo (isset($a_contact['product_name']) ? $a_contact['product_name'] : '') ?>
                                        </label>

                                    </div>
                                </div>


                            </div>
                            <h3 class="form-section">Mô tả chung</h3>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">

                                        <label class="control-label">
                                            <?php echo (isset($a_contact['description']) ? $a_contact['description'] : '') ?>                                            

                                        </label>
                                    </div>
                                </div>

                            </div>

                            <h3 class="form-section">Danh sách trả lời</h3>
                            <?php
                            $comments = get_all_comments($a_contact['id']);
                            if (count($comments) == 0) {
                                ?>
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="form-group">

                                            <label class="control-label">
                                                Chưa có câu trả lời nào

                                            </label>
                                        </div>
                                    </div>

                                </div>
                                <?php
                            } else {
                                foreach ($comments as $comment) {
                                    ?>
                                    <div class="row">
                                        <div class="col-md-2">
                                            <div class="form-group">

                                                <label class="control-label">
                                                    <?php
                                                    include_once(dirname(__FILE__) . '/../user/users_lib.php');
                                                    $user = get_user_detail($comment['user_comment']);
                                                    echo $user['first_name'] . ' ' . $user['last_name'];
                                                    ?>

                                                </label>
                                            </div>
                                        </div>
                                        <div class="col-md-2">
                                            <div class="form-group">

                                                <label class="control-label"> đã trả lời vào ngày
                                                    <?php
                                                    include_once(dirname(__FILE__) . '/../util.php');
                                                    if ($comment['date_created'] != null) {
                                                        echo make_readable_time($comment['date_created']);
                                                    }
                                                    ?>

                                                </label>
                                            </div>
                                        </div>
                                        <div class="col-md-8">
                                            <div class="form-group">
                                                Nội dung:
                                                <label class="control-label">
                                                    <?php echo $comment['comment'] ?>

                                                </label>
                                            </div>
                                        </div>

                                    </div>
                                    <?php
                                }
                            }
                            ?>
                            <h3 class="form-section">Nhập câu trả lời</h3>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">

                                        <textarea name="comment" rows="3" class="form-control"></textarea>

                                    </div>
                                </div>

                            </div>

                            <h3 class="form-section">Đã giải quyết xong</h3>
                            <div class="row">
                                <div class="col-md-12">
                                    <input type="checkbox" name="status" value="3"/>
                                </div>

                            </div>

                        </div>
                        <div class="form-actions right">
                            <button class="btn default" type="button">Hủy</button>
                            <input type="hidden" name="support_id" value="<?php echo (isset($a_contact['id']) ? $a_contact['id'] : '') ?>"/>
                            <input type="hidden" name="id" value="<?php echo (isset($a_contact['id']) ? $a_contact['id'] : '') ?>"/>
                            <button class="btn blue" type="submit" name="go"><i class="fa fa-check"></i> Lưu</button>
                        </div>
                    </form>
                    <!-- END FORM-->
                </div>
            </div>
            <a class="btn default" style="display:none" id="auto_fire_when_add_ok">

            </a>
            <!-- END PAGE CONTENT -->
        </div>
    </div>
    <!-- END CONTENT -->
</div>
<!-- END CONTAINER -->
<!-- BEGIN FOOTER -->
<?php
require_once('../footer.php');
require_once('../common_script.php');
?>
<!-- BEGIN PAGE LEVEL SCRIPTS -->
<script src="<?php echo $url ?>/assets/scripts/core/app.js"></script>
<script src="<?php echo $url ?>/js/add_comment_validation.js"></script>
<script src="<?php echo $url ?>/js/add_comment_alert_dialog.js"></script>
<script>
    jQuery(document).ready(function () {
        // initiate layout and plugins
        App.init();
        FormAddContactValidation.init();
        UIAlertDialogApi.init();
<?php
if (isset($_POST['go'])) {
    ?>
            $('#auto_fire_when_add_ok').click();
            //$('#auto_fire_when_add_ok').click({ id: '<?php echo $_POST['id'] ?>' }, handleClick);

    <?php
}
?>
    });
</script>
</body>
<!-- END BODY -->
</html>